package com.itbaizhan.myshiro.service;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/*认证的逻辑管理*/
@Service
public class UserService {
    @Autowired
    private DefaultWebSecurityManager securityManager;
    public void userLogin(String username,String password,String rememberMe) throws AuthenticationException {
        //将securityManager对象高置到运行环境中  因为在容器中没有这对象就没有办法运行
        SecurityUtils.setSecurityManager(securityManager);
        //获取subject对象  要使用它的认证方法
        Subject subject = SecurityUtils.getSubject();
        //将前端传来的用户名密码封装为shiro提供的身份对象
        // 因为subject要认证，而认证必须是shiro的身份对象
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
       //对rememberMe 进行判断
        if(rememberMe != null){
            token.setRememberMe(true);
        }

        // shiro认证
        subject.login(token);
    }
}
